<template>
  <div class="view-house">
    <div v-show="showTable">
    <el-table :data="tableData"
              border
              :fit="true"
              style="width: 100%">
      <el-table-column label="房屋id"
                       prop="houseId" width="80">
      </el-table-column>
      <el-table-column label="地址"
                       prop="address" width="280">
      </el-table-column>
      <el-table-column label="面积"
                       prop="area">
      </el-table-column>
      <el-table-column label="价格"
                       prop="price">
      </el-table-column>
      <el-table-column label="申请人姓名"
                         prop="applyName">
      </el-table-column>
      <el-table-column label="申请人身份证号码"
                       prop="applyNumber">
      </el-table-column>
      <el-table-column label="申请人电话号码"
                       prop="phone">
      </el-table-column>
      <el-table-column label="状态" v-if="this.role==='renter'">
                       <template slot-scope="scope">
                          <el-tag type="success">申请中</el-tag>
                       </template>
      </el-table-column>
      <el-table-column label="操作"  width="200px" v-if="this.role==='admin'">
        <template slot-scope="scope">
          <el-button size="mini"
                     type="primary"
                     @click="handleEdit(scope.$index, scope.row)">同意租赁</el-button>
          <el-button size="mini"
                     type="danger"
                     @click="handleDelete(scope.$index, scope.row)">拒绝租赁</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination-box">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="pagination.currentPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total">
      </el-pagination>
    </div>
  </div>
  <div v-show="showEditCon" class="edit-contract">
    <EditContract  @child-event="getChild()"></EditContract>
  </div>
  </div>
</template>
<script lang="js">
 import EditContract from '@/components/EditContract.vue'
 import axios from "axios";
export default {
  name: 'Houselist',
  components: {
      EditContract
    },
  data(){
    return {
      tableData: [],
        showEditCon: false,
        showTable: true,
        role: '',
      pagination: {//分页相关模型数据
        currentPage: 1,//当前页码
        pageSize: 10,//每页显示的记录数
        total: 0,//总记录数
        type:"",
        name:"",
        description:"",
      }
    }
  },
  created(){
    this.role = this.$store.state.userInfo.role;
    this.getAll()
  },
  methods: {
      getAll: function (){
        axios.get("/apply/"+this.pagination.currentPage).then((res)=>{
          if (res.data.code === 1){
            this.tableData = res.data.data.records
            this.pagination.currentPage = res.data.data.current
            this.pagination.total = res.data.data.total
            this.pagination.pageSize = res.data.data.size
          }
        })
      },
      handleEdit(index, row) {
        this.showEditCon = true;
        this.showTable = false;
      },
      handleDelete(index, row) {
          this.$confirm('确定要拒绝吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'error'
        }).then(() => {
            axios.delete("/apply/reject/"+row.houseId).then((res)=>{
              if (res.data.code ===1){
                this.$message({
                  type: 'success',
                  message: '拒绝成功!'
                });
              }else {
                this.$message.error(res.data.msg)
              }
            }).finally(this.getAll)
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消成功'
          });
        }).finally(this.getAll);
      },
      getChild(){
        this.showTable = true;
        this.showEditCon = false;
      },
    handleCurrentChange(currentPage) {
      this.pagination.currentPage = currentPage;
      this.getAll();
    },
    }
}
</script>
<style lang="scss" scoped>
</style>
